﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FindURL
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入需要处理的任务：");
            Console.WriteLine("1. 分析文章");
            Console.WriteLine("2. 处理文章");
            var key = Console.ReadKey();

            switch (key.KeyChar)
            {
                case '1':
                    Analysis();
                    break;
                case '2':
                    ArticleHandler();
                    break;
                default:
                    break;
            }
            //File.WriteAllText(fileName, result.ToString());
            Console.ReadKey();
        }

        static void ArticleHandler()
        {
            var fileName = "fileresult.txt";
            if (!File.Exists(fileName))
            {
                Console.WriteLine("需要处理的文件不存在");
                return;
            }

            string line;
            var pos = 0;
            string searchText = "当前处理ID：";
            string searchText2 = "文章路径：";
            var dic = new Dictionary<string, string>();
            using (StreamReader reader = new StreamReader(fileName))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.IndexOf(searchText) > -1)
                    {
                        pos = line.IndexOf('，');
                        var key = line.Substring(searchText.Length, pos - searchText.Length);
                        pos = line.IndexOf(searchText2) + searchText2.Length;
                        var content = line.Substring(pos);
                        if (!dic.ContainsKey(key))
                        {
                            dic.Add(key, content);
                        }
                    }
                }
                reader.Close();
            }

            if (dic.Count > 0)
            {
                Console.WriteLine("存在需要处理的文章，一共有{0}", dic.Count);
                DBHandler(dic);
                FileHandler(dic);
            }
        }

        static void DBHandler(Dictionary<string, string> dic)
        {
            var db = GetInstance();
            var list = (from item in dic
                        select item.Key).ToArray();
            db.Deleteable<Article>().In(list).ExecuteCommand();
        }

        static void FileHandler(Dictionary<string, string> dic)
        {
            var fileName = "filename.txt";
            var directoryName = "D:\\TBSWeb";
            var fileNames = new List<string>();

            if (File.Exists(fileName))
            {
                fileNames = File.ReadAllLines(fileName).ToList();
            }
            else
            {
                DirectoryInfo d = new DirectoryInfo(directoryName);
                FileInfo[] files = d.GetFiles("*.html", SearchOption.AllDirectories);//文件
                fileNames.AddRange(from item in files
                                   select item.FullName);
                File.WriteAllLines(fileName, fileNames);
            }

            foreach (var item in dic)
            {
                var itemFileName = item.Value.Replace('/', '\\');
                var file = fileNames.FirstOrDefault(o => o.Contains(itemFileName, StringComparison.OrdinalIgnoreCase));
                if (file != null)
                {
                    if (File.Exists(file))
                    {
                        File.Delete(file);
                    }
                    Console.WriteLine("删除{0}文件完成！{1}", item.Value, file);
                }
                else
                {
                    Console.WriteLine("找不到文件{0}", item.Value);
                }
            }
        }

        static void Director(string dir, List<string> list)
        {
            DirectoryInfo d = new DirectoryInfo(dir);
            FileInfo[] files = d.GetFiles("*.dll", SearchOption.AllDirectories);//文件
            DirectoryInfo[] directs = d.GetDirectories();//文件夹
            foreach (FileInfo f in files)
            {
                list.Add(f.Name);//添加文件名到列表中  
            }
            //获取子文件夹内的文件列表，递归遍历  
            foreach (DirectoryInfo dd in directs)
            {
                Director(dd.FullName, list);
            }
        }

        static void Analysis()
        {
            var db = GetInstance();
            var pageIndex = 1;
            var pageSize = 1000;
            var totalCount = 0;
            var page = db.Queryable<Article>()
                .ToPageList(pageIndex, pageSize, ref totalCount);
            var fileName = "result.txt";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            while (page.Count > 0)
            {
                page = db.Queryable<Article>()
                .ToPageList(pageIndex, pageSize, ref totalCount);
                pageIndex++;

                foreach (var item in page)
                {
                    StringBuilder result = new StringBuilder();
                    Regex reg = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    var content = System.Web.HttpUtility.HtmlDecode(item.Content);
                    MatchCollection mc = reg.Matches(content);
                    StringBuilder sb = new StringBuilder();
                    foreach (Match m in mc)
                    {
                        if (m.Value.IndexOf("tencent://message/?") > -1 ||
                            m.Value.IndexOf("tianyi-light") > -1 ||
                            m.Value.IndexOf("tbshops.com") > -1 ||
                            m.Value.IndexOf("tb888.net") > -1 ||
                            m.Value.IndexOf("mailto") > -1 ||
                            m.Value.IndexOf("163.com") > -1 ||
                            m.Value.IndexOf("wpa.qq.com") > -1 ||
                            m.Value.IndexOf("javascript:") > -1 ||
                            m.Value.IndexOf("cncsch.com") > -1 ||
                            m.Value.IndexOf("linkshop.com.cn") > -1)
                            continue;
                        reg = new Regex("\"([^\"]*)\"");
                        var mm = reg.Matches(m.Value);
                        foreach (Match mv in mm)
                        {
                            sb.AppendLine(Regex.Replace(mv.Value, @"[\""]+", ""));
                        }
                    }
                    Console.WriteLine("当前处理ID：{0}", item.Id);

                    if (sb.Length > 0)
                    {
                        Console.WriteLine("当前文章内容路径有：{0}", sb.ToString());
                        result.AppendFormat("当前处理ID：{0}，更新时间为：{1}", item.Id, item.UpdateTime);
                        result.AppendLine(string.Format("文章路径：{0}", item.LinkUrl));
                        result.AppendLine("当前文章内网址有");
                        result.AppendLine(sb.ToString());
                        result.AppendLine("----------------------------");
                        //Console.WriteLine(result.ToString());
                        File.AppendAllTextAsync(fileName, result.ToString());
                    }
                }
            }
        }

        /// <summary>
        /// Create SqlSugarClient
        /// </summary>
        /// <returns></returns>
        private static SqlSugarClient GetInstance()
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Server=127.0.0.1;uid=user;password=123456;database=db;",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });
            //Print sql
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            return db;
        }

    }

    [SugarTable("TB_article")]
    public class Article
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        public int? MemberId { get; set; }
        public string Content { get; set; }
        public DateTime AddTime { get; set; }
        public DateTime UpdateTime { get; set; }
        public string LinkUrl { get; set; }
    }
}
